package day_2022_12_to_3.year2023;

/**
 * @author haomin
 * @date 2023/03/25 09:10
 **/
public class Day3_25_Num1574 {
    class Solution {
        public int findLengthOfShortestSubarray(int[] arr) {
            // 从两边查询单调，然后枚举找到中间最短删除子数组 O(2n)
            int l = 0, r = arr.length - 1, n = r, min = n;
            while(l < n && arr[l] <= arr[l+1]) l++;
            while(0 < r && arr[r-1] <= arr[r]) r--;
            if(l >= r) return 0;
            if(arr[l] <= arr[r]) return r - l - 1;
            for(int i = l; i >= 0 && n >= r; --i) {
                while(n >= r && arr[i] <= arr[n]) n--;
                min = Math.min(min, n-i);
            }
            return Math.min(min, r);
        }
    }
}